home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / ConfigSystem / main.fx < prev    next >
Encoding:
Text File  |  2004-09-28  |  9.9 KB  |  329 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: ConfigSystem.fx
  3. //
  4. // The effect file for the ConfigSystem sample.  
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8.  
  9.  
  10. //--------------------------------------------------------------------------------------
  11. // Global variables
  12. //--------------------------------------------------------------------------------------
  13. matrix   g_mWorld;                  // World matrix for object
  14. matrix   g_mView;                   // View matrix for object
  15. matrix   g_mProj;                   // Projection matrix for object
  16. matrix   g_mWorldView;              // World * View matrix
  17. matrix   g_mWorldViewProj;          // World * View * Projection matrix
  18. texture  g_txScene;                 // texture for scene rendering
  19. float4   g_matDiffuse;              // Diffuse component of material
  20. float4   g_matSpecular;             // Specular component of material
  21. float    g_matPower;                // Specular power of material
  22. float4   g_vLightPos[4] = { float4(  3.5f, 1.0f,  5.5f, 1.0f ),
  23.                             float4( -3.5f, 1.0f, -5.5f, 1.0f ),
  24.                             float4(  3.5f, 1.0f, -5.5f, 1.0f ),
  25.                             float4( -3.5f, 1.0f,  5.5f, 1.0f ) };  // Light position in world space
  26. float4   g_vLightColor = float4( 0.5f, 0.5f, 0.5f, 1.0f );
  27.  
  28. // These parameters will change based on the config flags.
  29. bool     g_bUseSpecular = false;
  30. bool     g_bUseAnisotropic = false;
  31. int      g_MinFilter = 2;           // Minification filtering
  32. int      g_MaxAnisotropy = 1;       // Maximum anisotropy
  33.  
  34.  
  35. //-----------------------------------------------------------------------------
  36. // Texture samplers
  37. //-----------------------------------------------------------------------------
  38. sampler g_samScene =
  39. sampler_state
  40. {
  41.     Texture = <g_txScene>;
  42.     MinFilter = <g_MinFilter>;
  43.     MagFilter = Linear;
  44.     MipFilter = Linear;
  45.     MaxAnisotropy = <g_MaxAnisotropy>;
  46. };
  47.  
  48.  
  49. void VS20( float4 vPos : POSITION,
  50.            float3 vNormal : NORMAL,
  51.            float2 vTex0 : TEXCOORD0,
  52.            out float4 oPos : POSITION,
  53.            out float2 oTex0 : TEXCOORD0,
  54.            out float3 oVtE : TEXCOORD1,
  55.            out float3 oVNormal : TEXCOORD2,
  56.            out float3 oViewVtL0 : TEXCOORD3,
  57.            out float3 oViewVtL1 : TEXCOORD4,
  58.            out float3 oViewVtL2 : TEXCOORD5,
  59.            out float3 oViewVtL3 : TEXCOORD6 )
  60. {
  61.     oPos = mul( vPos, g_mWorldViewProj );
  62.  
  63.     // Compute view space normal
  64.     oVNormal = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  65.  
  66.     // Vertex pos in view space (normalize in pixel shader)
  67.     oVtE = -mul( vPos, g_mWorldView );
  68.  
  69.     // Compute view space vertex to light vectors (normalized)
  70.     oViewVtL0 = normalize( mul( g_vLightPos[0], g_mView ) + oVtE );
  71.     oViewVtL1 = normalize( mul( g_vLightPos[1], g_mView ) + oVtE );
  72.     oViewVtL2 = normalize( mul( g_vLightPos[2], g_mView ) + oVtE );
  73.     oViewVtL3 = normalize( mul( g_vLightPos[3], g_mView ) + oVtE );
  74.  
  75.     // Propogate texcoords
  76.     oTex0 = vTex0;
  77. }
  78.  
  79.  
  80. float4 PS20( float2 Tex0 : TEXCOORD0,
  81.              float3 VtE : TEXCOORD1,
  82.              float3 N : TEXCOORD2,
  83.              float3 VtL0 : TEXCOORD3,
  84.              float3 VtL1 : TEXCOORD4,
  85.              float3 VtL2 : TEXCOORD5,
  86.              float3 VtL3 : TEXCOORD6 ) : COLOR0
  87. {
  88.     // Diffuse luminance
  89.     float LumD = max( 0.0f, dot( N, VtL0 ) ) +
  90.                  max( 0.0f, dot( N, VtL1 ) ) +
  91.                  max( 0.0f, dot( N, VtL2 ) ) +
  92.                  max( 0.0f, dot( N, VtL3 ) );
  93.  
  94.     // Normalize view space vertex-to-eye
  95.     VtE = normalize( VtE );
  96.  
  97.     // Specular luminance
  98.     float LumS = pow( max( 0.0f, dot( N, normalize( VtE + VtL0 ) ) ), g_matPower ) +
  99.                  pow( max( 0.0f, dot( N, normalize( VtE + VtL1 ) ) ), g_matPower ) +
  100.                  pow( max( 0.0f, dot( N, normalize( VtE + VtL2 ) ) ), g_matPower ) +
  101.                  pow( max( 0.0f, dot( N, normalize( VtE + VtL3 ) ) ), g_matPower );
  102.  
  103.     float Specular;
  104.     if( g_bUseSpecular )
  105.         Specular = g_matSpecular * LumS * g_vLightColor;
  106.     else
  107.         Specular = 0.0f;
  108.  
  109.     return tex2D( g_samScene, Tex0 ) * g_matDiffuse * LumD * g_vLightColor + Specular;
  110. }
  111.  
  112.  
  113. void VS( float4 vPos : POSITION,
  114.          float3 vNormal : NORMAL,
  115.          float2 vTex0 : TEXCOORD0,
  116.          out float4 oDiffuse : COLOR0,
  117.          out float4 oSpecular : COLOR1,
  118.          out float4 oPos : POSITION,
  119.          out float2 oTex0 : TEXCOORD0 )
  120. {
  121.     oPos = mul( vPos, g_mWorldViewProj );
  122.  
  123.     // Compute view space normal
  124.     float3 N = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  125.  
  126.     // Vertex pos in view space
  127.     float3 Vv = mul( vPos, g_mWorldView );
  128.  
  129.     float LumD = 0.0f;  // Diffuse luminance
  130.     float LumS = 0.0f;  // Specular luminance
  131.     for( int i = 0; i < 4; ++i )
  132.     {
  133.         // Light pos in view space
  134.         float3 Lv = mul( g_vLightPos[i], g_mView );
  135.  
  136.         // View space vertex-to-light
  137.         float3 VtL = normalize( Lv - Vv );
  138.  
  139.         LumD += max( 0.0f, dot( N, VtL ) );
  140.  
  141.         // View space vertex-to-eye
  142.         float3 VtE = normalize( -Vv );
  143.  
  144.         // Half vector
  145.         float3 H = normalize( VtE + VtL );
  146.  
  147.         LumS += pow( max( 0.0f, dot( N, H ) ), 40.0f ); // This power is hardcoded to not exceed inst limit.
  148.     }
  149.  
  150.     oDiffuse = g_matDiffuse * LumD * g_vLightColor;
  151.     if( g_bUseSpecular )
  152.         oSpecular = g_matSpecular * LumS * g_vLightColor;
  153.     else
  154.         oSpecular = 0.0f;
  155.  
  156.     oTex0 = vTex0;
  157. }
  158.  
  159.  
  160. float4 PS( float4 Diffuse : COLOR0,
  161.            float4 Specular : COLOR1,
  162.            float2 Tex0 : TEXCOORD0 ) : COLOR0
  163. {
  164.     return tex2D( g_samScene, Tex0 ) * Diffuse + Specular;
  165. }
  166.  
  167.  
  168. //--------------------------------------------------------------------------------------
  169. // Techniques
  170. //--------------------------------------------------------------------------------------
  171. technique RenderScenePS30
  172. {
  173.     pass P0
  174.     {
  175.         VertexShader = compile vs_3_0 VS20();
  176.         PixelShader = compile ps_3_0 PS20();
  177.     }
  178. }
  179.  
  180.  
  181. technique RenderScenePS20A
  182. {
  183.     pass P0
  184.     {
  185.         VertexShader = compile vs_2_0 VS20();
  186.         PixelShader = compile ps_2_a PS20();
  187.     }
  188. }
  189.  
  190.  
  191. technique RenderScenePS20B
  192. {
  193.     pass P0
  194.     {
  195.         VertexShader = compile vs_2_0 VS20();
  196.         PixelShader = compile ps_2_b PS20();
  197.     }
  198. }
  199.  
  200.  
  201. technique RenderScenePS20
  202. {
  203.     pass P0
  204.     {
  205.         VertexShader = compile vs_2_0 VS20();
  206.         PixelShader = compile ps_2_0 PS20();
  207.     }
  208. }
  209.  
  210.  
  211. technique RenderScenePS14
  212. {
  213.     pass P0
  214.     {
  215.         VertexShader = compile vs_1_1 VS();
  216.         PixelShader = compile ps_1_4 PS();
  217.     }
  218. }
  219.  
  220.  
  221. technique RenderScenePS13
  222. {
  223.     pass P0
  224.     {
  225.         VertexShader = compile vs_1_1 VS();
  226.         PixelShader = compile ps_1_3 PS();
  227.     }
  228. }
  229.  
  230.  
  231. technique RenderScenePS12
  232. {
  233.     pass P0
  234.     {
  235.         VertexShader = compile vs_1_1 VS();
  236.         PixelShader = compile ps_1_2 PS();
  237.     }
  238. }
  239.  
  240.  
  241. technique RenderScenePS11
  242. {
  243.     pass P0
  244.     {
  245.         VertexShader = compile vs_1_1 VS();
  246.         PixelShader = compile ps_1_1 PS();
  247.     }
  248. }
  249.  
  250.  
  251. technique FFRenderScene
  252. {
  253.     pass P0
  254.     {
  255.         VertexShader = null;
  256.         PixelShader = null;
  257.  
  258.         WorldTransform[0] = <g_mWorld>;
  259.         ViewTransform = <g_mView>;
  260.         ProjectionTransform = <g_mProj>;
  261.  
  262.         Texture[0] = <g_txScene>;
  263.         MinFilter[0] = <g_MinFilter>;
  264.         MaxAnisotropy[0] = <g_MaxAnisotropy>;
  265.         MagFilter[0] = Linear;
  266.         MipFilter[0] = Linear;
  267.         AddressU[0] = Wrap;
  268.         AddressV[0] = Wrap;
  269.         AddressW[0] = Wrap;
  270.         ColorArg1[0] = Texture;
  271.         ColorArg2[0] = Current;
  272.         ColorOp[0] = Modulate;
  273.  
  274.         Lighting = True;
  275.         LightEnable[0] = True;
  276. //        LightAmbient[0] = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  277.         LightType[0] = Point;
  278.         LightPosition[0] = <g_vLightPos[0]>;
  279.         LightRange[0] = 100.0f;
  280.         LightDiffuse[0] = <g_vLightColor>;
  281.         LightSpecular[0] = <g_vLightColor>;
  282.         LightAttenuation0[0] = 1.0f;
  283.         LightAttenuation1[0] = 0.0f;
  284.         LightAttenuation2[0] = 0.0f;
  285.         LightEnable[1] = True;
  286. //        LightAmbient[1] = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  287.         LightType[1] = Point;
  288.         LightPosition[1] = <g_vLightPos[1]>;
  289.         LightRange[1] = 100.0f;
  290.         LightDiffuse[1] = <g_vLightColor>;
  291.         LightSpecular[1] = <g_vLightColor>;
  292.         LightAttenuation0[1] = 1.0f;
  293.         LightAttenuation1[1] = 0.0f;
  294.         LightAttenuation2[1] = 0.0f;
  295.         LightEnable[2] = True;
  296. //        LightAmbient[2] = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  297.         LightType[2] = Point;
  298.         LightPosition[2] = <g_vLightPos[2]>;
  299.         LightRange[2] = 100.0f;
  300.         LightDiffuse[2] = <g_vLightColor>;
  301.         LightSpecular[2] = <g_vLightColor>;
  302.         LightAttenuation0[2] = 1.0f;
  303.         LightAttenuation1[2] = 0.0f;
  304.         LightAttenuation2[2] = 0.0f;
  305.         LightEnable[3] = True;
  306. //        LightAmbient[3] = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  307.         LightType[3] = Point;
  308.         LightPosition[3] = <g_vLightPos[3]>;
  309.         LightRange[3] = 100.0f;
  310.         LightDiffuse[3] = <g_vLightColor>;
  311.         LightSpecular[3] = <g_vLightColor>;
  312.         LightAttenuation0[3] = 1.0f;
  313.         LightAttenuation1[3] = 0.0f;
  314.         LightAttenuation2[3] = 0.0f;
  315.  
  316.         NormalizeNormals = true;
  317.  
  318.         SpecularEnable = <g_bUseSpecular>;
  319.         Ambient = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  320.         DiffuseMaterialSource = Material;
  321.         SpecularMaterialSource = Material;
  322.         MaterialAmbient = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  323.         MaterialDiffuse = <g_matDiffuse>;
  324.         MaterialEmissive = float4( 0.0f, 0.0f, 0.0f, 0.0f );
  325.         MaterialSpecular = <g_matSpecular>;
  326.         MaterialPower = <g_matPower>;
  327.     }
  328. }
  329.